package stz.flash.option;

import net.rim.device.api.system.Characters;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Keypad;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.Ui;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.UiEngine;
import net.rim.device.api.ui.XYDimension;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.FullScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.Background;
import net.rim.device.api.ui.decor.BackgroundFactory;
import net.rim.device.api.ui.decor.Border;
import net.rim.device.api.ui.decor.BorderFactory;
import stz.flash.main.FlashTimer;
import stz.flash.main.ScreenHandle;
import stz.flash.main.strobe.model.VRFlashController;
import stz.flash.option.app.BackgroundExecuteCheckBox;
import stz.flash.option.app.DoubleClickStartKeyChoiceField;
import stz.flash.option.app.HomeScreenHotkeyChoiceField;
import stz.flash.option.app.StartupModeChoiceField;
import stz.flash.option.app.UninstallField;
import stz.flash.option.app.VibrationCheckBox;
import stz.flash.option.flashlight.BacklightStartupStateCheckbox;
import stz.flash.option.flashlight.FlashlightOption;
import stz.flash.option.flashlight.FlashlightStartupStateCheckbox;
import stz.flash.option.flashlight.LEDStartupStateCheckbox;
import stz.flash.option.strobe.StrobeTimeIntervalManager;
import stz.flash.option.strobe.StrobeOnTimeManager;
import stz.flash.option.strobe.StrobeToggleKeyChoiceField;
import stz.flash.string.OptionString;
import blackberry.util.DjColorData;
import blackberry.util.DjFontData;
import blackberry.util.field.DjLabelField;


public class OptionScreen extends FullScreen {

	private static final Background subjectBackground = BackgroundFactory.createLinearGradientBackground(
			Color.DARKSLATEGRAY,
			Color.TEAL,
			Color.DARKSLATEGRAY,
			Color.DARKSLATEGRAY);
	private static final Border subjectBorder = BorderFactory.createSimpleBorder(
			new XYEdges(1, 0, 0, 0),
			new XYEdges(Color.DARKSLATEGRAY, 0, 0, 0),
			Border.STYLE_SOLID);
	private static final int[] ignoreKeys = {Keypad.KEY_CONVENIENCE_1, Keypad.KEY_CONVENIENCE_2};
	
	private static OptionScreen instance = new OptionScreen();
	
	//App
	private DoubleClickStartKeyChoiceField doubleClickStartKeyChoiceField = new DoubleClickStartKeyChoiceField();
	private HomeScreenHotkeyChoiceField homeScreenHotkeyChoiceField = new HomeScreenHotkeyChoiceField();;
	private StartupModeChoiceField startupModeChoiceField = new StartupModeChoiceField();
	private VibrationCheckBox vibrationCheckbox = new VibrationCheckBox();
	private BackgroundExecuteCheckBox backgroundExecuteCheckbox = new BackgroundExecuteCheckBox();
	private UninstallField uninstallField = new UninstallField();
	
	
	//Flashlight Mode 
	private TimerDurationChoiceField timerDurationChoiceField = TimerDurationChoiceField.getIns();
	private LEDStartupStateCheckbox ledStartupStateChoiceField = new LEDStartupStateCheckbox();
	private BacklightStartupStateCheckbox backlightStartupStateChoiceField = new BacklightStartupStateCheckbox();
	private FlashlightStartupStateCheckbox flashStartupStateChoiceField = new FlashlightStartupStateCheckbox();
	
	
	//Strobe Mode
	private StrobeToggleKeyChoiceField strobeToggleKeyChoiceField = new StrobeToggleKeyChoiceField();
	private StrobeOnTimeManager strobeOnTimeManager = new StrobeOnTimeManager();
	private StrobeTimeIntervalManager strobeTimeIntervalManager = StrobeTimeIntervalManager.getIns();

	
	private OptionScreen() {
		super(new VerticalFieldManager(Manager.VERTICAL_SCROLL), 0);
		
		add(new SubjectManager(OptionString.SUBJECT_1));
		
		add(doubleClickStartKeyChoiceField);
		addSeparator();
		
		add(homeScreenHotkeyChoiceField);
		addSeparator();
		
		add(timerDurationChoiceField);
		addSeparator();
		
		add(startupModeChoiceField);
		addSeparator();
		
		add(vibrationCheckbox);
		addSeparator();
		
		add(backgroundExecuteCheckbox);
		addSeparator();
		
		add(uninstallField);

		
		add(new SubjectManager(OptionString.SUBJECT_2));
		
		add(ledStartupStateChoiceField);
		addSeparator();
		
		add(backlightStartupStateChoiceField);
		addSeparator();

		add(flashStartupStateChoiceField);
		
		
		add(new SubjectManager(OptionString.SUBJECT_3));
		
		add(strobeToggleKeyChoiceField);
		addSeparator();
		
		add(strobeOnTimeManager);
		addSeparator();

		add(strobeTimeIntervalManager);
	}
	private void addSeparator() {
		add(new SeparatorField());
	}
	
	public static OptionScreen getIns() {
		return instance;
	}

	public static boolean isOptionDisplayed(){
		return instance.isDisplayed();
	}
	
	public static void pushScreen(){
		if (instance.isDisplayed()) 
			return;
		UiApplication.getUiApplication().pushGlobalScreen(
				instance, 
				ScreenHandle.PRIORITY_OPTION_SCREEN, 
		UiEngine.GLOBAL_SHOW_LOWER);
	}
	
	protected boolean keyDown(int keycode, int j) {
		
		if (isIgnoreKeys(keycode))
			return true;

		if (Keypad.key(keycode) == Characters.ESCAPE || Keypad.key(keycode) == Keypad.KEY_END) {
					
			if (isLatestModeFlashlight()) {
				FlashlightOption.getIns().updateFlashStateWithStartupSet();
				FlashTimer.getIns().stopAndStart(FlashTimer.STOP_AND_START_into_flashlight_mode);
				close();

			} else {
				FlashTimer.getIns().stopAndStart(FlashTimer.STOP_AND_START_into_strobe_mode);
				close();
				VRFlashController.applyVRFlashStateOfOption();
			}
		}
		return super.keyDown(keycode, j);
	}
	private boolean isIgnoreKeys(int keycode){
		int length = ignoreKeys.length;
		int key = Keypad.key(keycode);
		for (int i = 0; i < length; i++){
			if (key == ignoreKeys[i])
				return true;
		}
		return false;
	}
	private boolean isLatestModeFlashlight() {
		return Option.getIns().getLatestMode() == Option.FLASHLIGHT_MODE;
	}
	
	protected void sublayout(int width, int height) {
		//main화면이 조금이라도 보여야 option을 닫을때 깔끔하게 닫힘
		super.sublayout(width, height - 2);
	}
	
	static class SubjectManager extends Manager {

		private static int MANAGER_HEIGHT_mm = 3;
		private static int MANAGER_HEIGHT_px = 
				Ui.convertSize(MANAGER_HEIGHT_mm, Ui.UNITS_mm, Ui.UNITS_px);
		
		private static int LABEL_HEIGHT_pt = 7;
		
		private DjLabelField subjectLabelField;
		
		public SubjectManager(String label) {
			super(0);

			subjectLabelField =  DjLabelField.createColorNewFontLabel(
					label, 
					new DjColorData(Color.WHITE, DjColorData.DEFAULT),
					Font.getDefault().derive(Font.PLAIN, LABEL_HEIGHT_pt, Ui.UNITS_pt),
					0);
			
			setBackground(subjectBackground);
		
			setBorder(subjectBorder);
			
			add(subjectLabelField);
		}
		
		protected void sublayout(int maxWidth, int maxHeight) {
	
			setExtent(Display.getWidth(), MANAGER_HEIGHT_px);
			
			setPositionChild(subjectLabelField, 
					0,
					(MANAGER_HEIGHT_px - subjectLabelField.getPreferredHeight()) / 2);
			layoutChild(subjectLabelField,
					subjectLabelField.getPreferredWidth(),
					subjectLabelField.getPreferredHeight());
		}
	}
}



